package com.snake.vargoappstore.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.snake.mylibrary.base.BaseFragment;
import com.snake.mylibrary.loader.ArrayListAdapter;
import com.snake.mylibrary.loader.DynamicResult;
import com.snake.mylibrary.utils.LogUtil;
import com.snake.vargoappstore.R;
import com.snake.vargoappstore.db.AppInfo;
import com.snake.vargoappstore.loader.ClassFragmentLoader;
import com.snake.vargoappstore.result.ClassFragmentResult;
import com.snake.vargoappstore.widget.ListViewItem;


public class ClassFragment extends BaseFragment {


    private View mView;
    private ListView mListView;
    private ProgressBar mPb;
    private ClassFragmentListAdapter mClassFragmentListAdapter;

    @Override
    protected void initView() {
        mListView = (ListView) mView.findViewById(R.id.class_fragment_lv);
        mPb = (ProgressBar) mView.findViewById(R.id.class_fragment_pb);
    }

    @Override
    protected void initData() {
        mClassFragmentListAdapter = new ClassFragmentListAdapter(getActivity());
        mListView.setAdapter(mClassFragmentListAdapter);
        mListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        ClassFragmentLoaderCallBacks classFragmentLoaderCallBacks = new ClassFragmentLoaderCallBacks();
        getLoaderManager().initLoader(1002,null,classFragmentLoaderCallBacks);
        mPb.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected View getFragmentView() {
        LogUtil.e("ClassFragment");
        mView = View.inflate(getActivity(), R.layout.class_fragment, null);
        return mView;
    }

    class ClassFragmentListAdapter extends ArrayListAdapter<AppInfo>{

        public ClassFragmentListAdapter(Context context) {
            super(context);
        }

        @Override
        public View onCreateView(Context context, ViewGroup parent) {
            return new ListViewItem(getActivity());
        }

        @Override
        public void onBindData(View view, int position, AppInfo data) {
            ((ListViewItem)view).bindData(position,data);
        }
    }

    class ClassFragmentLoaderCallBacks implements LoaderManager.LoaderCallbacks<DynamicResult> {

        @Override
        public Loader<DynamicResult> onCreateLoader(int id, Bundle args) {
            return new ClassFragmentLoader(getActivity());
        }

        @Override
        public void onLoadFinished(Loader<DynamicResult> loader, DynamicResult data) {
            ClassFragmentResult result = (ClassFragmentResult) data;
            mPb.setVisibility(View.GONE);
            mClassFragmentListAdapter.updateData(result.getAppInfos());
        }

        @Override
        public void onLoaderReset(Loader<DynamicResult> loader) {

        }
    }
}
